import { showToast } from '@/components/Toast';
import { request } from '@/utils/front';

type ProductRes =
  | {
      data: any;
      success: boolean;
      message: string;
    }
  | Obj;

export async function getCategories() {
  const res = await request.get<ProductRes, ProductRes>('/v2/categories');

  if (res?.success) {
    return res.data;
  } else if (res?.message) {
    showToast({ type: 'error', message: res?.message });
    return false;
  } else {
    showToast({ type: 'error', message: 'network error' });
    return false;
  }
}

export async function getProducts(params: any) {
  const querys = new URLSearchParams(params).toString();
  const res = await request.get<ProductRes, ProductRes>(
    '/end/products' + (querys ? '?' + querys : '')
  );
  if (res?.success) {
    return res.data;
  } else if (res?.message) {
    showToast({ type: 'error', message: res?.message });
    return false;
  } else {
    showToast({ type: 'error', message: 'network error' });
    return false;
  }
}

export async function getProductsCount(params: any) {
  const querys = new URLSearchParams(params).toString();
  const res = await request.get<ProductRes, ProductRes>(
    '/v2/categories/count' + (querys ? '?' + querys : '')
  );
  if (res?.success) {
    return res.data;
  } else if (res?.message) {
    showToast({ type: 'error', message: res?.message });
    return false;
  } else {
    showToast({ type: 'error', message: 'network error' });
    return false;
  }
}

export async function getProductDetail(params: any) {
  const querys = new URLSearchParams(params); //.toString();
  const res = await request.get<ProductRes, ProductRes>(
    '/product/detail/' + querys.get('skus') // + (querys ? '?' + querys : '')
  );
  if (res?.success) {
    return res.data?.result;
  } else if (res?.message) {
    showToast({ type: 'error', message: res?.message });
    return false;
  } else {
    showToast({ type: 'error', message: 'network error' });
    return false;
  }
}

export async function getZoneRateCode(params: any) {
  const querys = new URLSearchParams(params);
  if (!querys.get('state')) querys.set('state', 'AU');
  const res = await request.get<ProductRes, ProductRes>(
    '/v2/countries/' +
      querys?.get('state') +
      '/postcodes/' +
      querys?.get('postcode') +
      '/zones'
  );
  if (res?.success) {
    return res.data;
  } else if (res?.message) {
    showToast({ type: 'error', message: res?.message });
    return false;
  } else {
    showToast({ type: 'error', message: 'network error' });
    return false;
  }
}

export async function postAddMySku(params: any) {
  const res = await request.post<ProductRes, ProductRes>(
    '/v2/my_sku/add',
    params
  );
  if (res?.success) {
    return res.data;
  } else if (res?.message) {
    showToast({ type: 'error', message: res?.message });
    return false;
  } else {
    showToast({ type: 'error', message: 'network error' });
    return false;
  }
}

export async function getCheckMySku(params: any) {
  const querys = new URLSearchParams(params).toString();
  const res = await request.get<ProductRes, ProductRes>(
    '/v2/my_sku/get' + (querys ? '?' + querys : '')
  );
  if (res?.success) {
    return res.data;
  } else if (res?.message) {
    showToast({ type: 'error', message: res?.message });
    return false;
  } else {
    showToast({ type: 'error', message: 'network error' });
    return false;
  }
}
